//
//  MineViewController.m
//  EastMarket
//
//  Created by 李鹏程 on 16/10/12.
//  Copyright © 2016年 guoguo. All rights reserved.
//

#import "MineViewController.h"

@interface MineViewController ()

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"个人中心";
    [self setupUI];
//    [self loadViewCurrentInfo];
}

- (void)setupUI {
    
    CGFloat border = 1.0f;
    //    CGFloat lblHeight = 44.0f;
    CGFloat spacing = 8.0f;
    CGFloat upperSpacing = 22.0f;
    // 背景ScrollView
    UIScrollView *applicationSV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    applicationSV.showsVerticalScrollIndicator = YES;
    applicationSV.showsVerticalScrollIndicator = NO;
    applicationSV.backgroundColor = [UIColor clearColor];
    //    applicationSV.delegate = self;
    applicationSV.scrollEnabled = YES;
    [self.view addSubview:applicationSV];
    
    CGFloat viewHeight = 44.0f;
    
    UIView *viewmyOrders = [[UIView alloc] initWithFrame:CGRectMake(0, upperSpacing, SCREEN_WIDTH, viewHeight)];
    viewmyOrders.backgroundColor = [UIColor whiteColor];
    [applicationSV addSubview:viewmyOrders];
    [self addSubDetailViews:viewmyOrders iconName:@"" iconLeft:8 title:@"我的订单" titleLeft:20 hint:@""];
    UITapGestureRecognizer *tapViewMyInsure = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewmyOrders:)];
    [viewmyOrders addGestureRecognizer:tapViewMyInsure];
    
    UIView *viewWaitingPayOrders = [[UIView alloc] initWithFrame:CGRectMake(0, viewmyOrders.frame.origin.y + viewmyOrders.frame.size.height + border, SCREEN_WIDTH, viewHeight)];
    viewWaitingPayOrders.backgroundColor = [UIColor whiteColor];
    [applicationSV addSubview:viewWaitingPayOrders];
    [self addSubDetailViews:viewWaitingPayOrders iconName:@"" iconLeft:8 title:@"待付款订单" titleLeft:20 hint:@""];
    UITapGestureRecognizer *tapViewApplication = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewWaitingPayOrders:)];
    [viewWaitingPayOrders addGestureRecognizer:tapViewApplication];
    
    UIView *viewWaitingConfirm = [[UIView alloc] initWithFrame:CGRectMake(0, viewWaitingPayOrders.frame.origin.y + viewWaitingPayOrders.frame.size.height + border, SCREEN_WIDTH, viewHeight)];
    viewWaitingConfirm.backgroundColor = [UIColor whiteColor];
    [applicationSV addSubview:viewWaitingConfirm];
    [self addSubDetailViews:viewWaitingConfirm iconName:@"" iconLeft:8 title:@"待确认订单" titleLeft:20 hint:@""];
    UITapGestureRecognizer *tapViewHisIncome = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewWaitingConfirm:)];
    [viewWaitingConfirm addGestureRecognizer:tapViewHisIncome];
    
    UIView *viewAllOrders = [[UIView alloc] initWithFrame:CGRectMake(0, viewWaitingConfirm.frame.origin.y + viewWaitingConfirm.frame.size.height + border, SCREEN_WIDTH, viewHeight)];
    viewAllOrders.backgroundColor = [UIColor whiteColor];
    [applicationSV addSubview:viewAllOrders];
    [self addSubDetailViews:viewAllOrders iconName:@"" iconLeft:8 title:@"待取消订单" titleLeft:20 hint:@""];
    UITapGestureRecognizer *tapViewComPro = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewAllOrders:)];
    [viewAllOrders addGestureRecognizer:tapViewComPro];
    
    UIView *viewUserInfo = [[UIView alloc] initWithFrame:CGRectMake(0, viewAllOrders.frame.origin.y + viewAllOrders.frame.size.height + border + upperSpacing, SCREEN_WIDTH, viewHeight)];
    viewUserInfo.backgroundColor = [UIColor whiteColor];
    [applicationSV addSubview:viewUserInfo];
    [self addSubDetailViews:viewUserInfo iconName:@"" iconLeft:8 title:@"个人信息" titleLeft:20 hint:nil];
    UITapGestureRecognizer *tapViewUserInfo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewUserInfo:)];
    [viewUserInfo addGestureRecognizer:tapViewUserInfo];
    
    UIView *viewChangePsd = [[UIView alloc] initWithFrame:CGRectMake(0, viewUserInfo.frame.origin.y + viewUserInfo.frame.size.height + border, SCREEN_WIDTH, viewHeight)];
    viewChangePsd.backgroundColor = [UIColor whiteColor];
    [applicationSV addSubview:viewChangePsd];
    [self addSubDetailViews:viewChangePsd iconName:@"" iconLeft:8 title:@"修改密码" titleLeft:20 hint:nil];
    UITapGestureRecognizer *tapViewChangePsd = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewChangePsd:)];
    [viewChangePsd addGestureRecognizer:tapViewChangePsd];

    UIView *viewConsumerHotline = [[UIView alloc] initWithFrame:CGRectMake(0, viewChangePsd.frame.origin.y + viewChangePsd.frame.size.height + border + upperSpacing, SCREEN_WIDTH, viewHeight)];
    viewConsumerHotline.backgroundColor = [UIColor whiteColor];
    [applicationSV addSubview:viewConsumerHotline];
    [self addSubDetailViews:viewConsumerHotline iconName:@"" iconLeft:8 title:@"客服电话" titleLeft:20 hint:@"4006-580-550"];
    UITapGestureRecognizer *tapViewConsumerHotline = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewConsumerHotline:)];
    [viewConsumerHotline addGestureRecognizer:tapViewConsumerHotline];
    
    //bottomView
    UIView *viewBottom = [[UIView alloc] initWithFrame:CGRectMake(0, viewConsumerHotline.frame.origin.y + viewConsumerHotline.frame.size.height + spacing, SCREEN_WIDTH, SCREEN_HEIGHT / 4 + spacing * 2)];
    viewBottom.backgroundColor = [UIColor clearColor];
    [applicationSV addSubview:viewBottom];
    
    UIButton *btnLoginOut = [UIButton buttonWithType:UIButtonTypeSystem];
    btnLoginOut.frame = CGRectMake(spacing, viewBottom.frame.size.height * 2 / 5, viewBottom.frame.size.width - 2 * spacing, viewBottom.frame.size.height  / 5);
    [btnLoginOut setTitle:@"退出登录" forState:UIControlStateNormal];
    [btnLoginOut setBackgroundColor:EastMarket_MainToneColor];
    [btnLoginOut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnLoginOut addTarget:self action:@selector(btnLoginOutClicked:) forControlEvents:UIControlEventTouchUpInside];
    btnLoginOut.layer.cornerRadius = 5.0f;
    [viewBottom addSubview:btnLoginOut];
    
    applicationSV.contentSize = CGSizeMake(SCREEN_WIDTH, viewBottom.frame.origin.y + viewBottom.frame.size.height + 8);
}

//各条件view块添加详细内容
-(UILabel *)addSubDetailViews:(UIView *)parentView iconName:(NSString *)imageName iconLeft:(CGFloat)iconX title:(NSString*)titleName titleLeft:(CGFloat)titleX hint:(NSString*)hintText{
    //开头图标
    UIImageView *viewIcon = [[UIImageView alloc] initWithFrame:CGRectMake(iconX, 0, 20, 20)];
    [parentView addSubview:viewIcon];
    viewIcon.center = CGPointMake(viewIcon.center.x, parentView.frame.size.height/2);
    [viewIcon setImage:[UIImage imageNamed:imageName]];
    
    //标题
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(titleX, 0, 64 + 20, 32)];
    [parentView addSubview:lblTitle];
    lblTitle.center = CGPointMake(lblTitle.center.x, parentView.frame.size.height/2);
    lblTitle.numberOfLines = 1;
    lblTitle.text = titleName;
    lblTitle.lineBreakMode = NSLineBreakByWordWrapping;
    //lblTitle.textColor = [UIColor colorWithRed:167.0/255 green:167.0/255 blue:167.0/255 alpha:1];
    [lblTitle setTextColor:[UIColor blackColor]];
    lblTitle.backgroundColor =[UIColor clearColor];
    lblTitle.font = [UIFont fontWithName:@"Arial" size:16];
    
    //右箭头  15*15，距离右边也是15
    UIImageView *viewArrow = [[UIImageView alloc] initWithFrame:CGRectMake(parentView.frame.size.width-15-15, 0, 15, 15)];
    
    viewArrow.center = CGPointMake(viewArrow.center.x, parentView.frame.size.height/2);
    [viewArrow setImage:[UIImage imageNamed:@"rightArrow"]];
    
    //内容，初始化时为提示文字
    CGFloat left = 62;
    if ([GlobalUnit isBlankString:hintText]) {
        [parentView addSubview:viewArrow];
    } else {
        
        left = SCREEN_WIDTH - 64 - 64 - 22;
    }
    
    UILabel *lblContent = [[UILabel alloc] initWithFrame:CGRectMake(left, 0, viewArrow.frame.origin.x - left - 15 + 15 + 16, 22 + 8)];
    [parentView addSubview:lblContent];
    lblContent.center = CGPointMake(lblContent.center.x, parentView.frame.size.height/2);
    lblContent.text = hintText;
        lblContent.backgroundColor =[UIColor clearColor];
    lblContent.textAlignment = NSTextAlignmentRight;
    lblContent.font = [UIFont fontWithName:@"Arial" size:16];
    
    if ([GlobalUnit isBlankString:hintText]) {
        
        lblContent.textColor = EastMarket_darkGreyFontColor;
    } else {
        
        lblContent.textColor = EastMatket_ConsumerHotLineColor;
    }
    return lblContent;
}

- (void)loadViewCurrentInfo {
    
    [NSThread detachNewThreadSelector:@selector(callServeretCurrentInfo) toTarget:self withObject:Nil];
}

-(void)callServeretCurrentInfo
{
    NSDictionary *dic = [AppServiceInterface GetCurrentAccountInfo];
    debugLog(@"%@", [dic objectForKey:@"success"]);
    
    [self performSelectorOnMainThread:@selector(updateUIAftGetCurrentAccountInfo:) withObject:dic waitUntilDone:false];
}

-(void)updateUIAftGetCurrentAccountInfo:(NSDictionary *)dic {
    
    debugLog(@"%@",dic);
    if (dic && [[dic objectForKey:@"success"] integerValue] == 1) {
        
    } else if (dic && !([[dic objectForKey:@"success"] integerValue] == 1)) {
        
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", [dic objectForKey:@"msg"]]];
        return;
    } else {
        [SVProgressHUD showErrorWithStatus:@"连接服务器失败，请稍后重试"];
        return;
    }
}

//各条件view块添加详细内容
-(UILabel *)addSubDetailViews:(UIView *)parentView iconName:(NSString *)imageName title:(NSString*)titleName hint:(NSString*)hintText{
    
    return [self addSubDetailViews:parentView iconName:imageName iconLeft:8 title:titleName titleLeft:16 hint:hintText];
}

- (void)btnLoginOutClicked:(UIButton *)btn {

    debugMethod();
    [self removeLoginSession];
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [loginVC setHidesBottomBarWhenPushed:true];
    [self.navigationController pushViewController:loginVC animated:true];
}

- (void)tapViewmyOrders:(UIGestureRecognizer *)tap {

    debugMethod();
    MyOrderViewController *myOrderVC = [[MyOrderViewController alloc] init];
    [myOrderVC setHidesBottomBarWhenPushed:true];
    [self.navigationController pushViewController:myOrderVC animated:true];
}

- (void)tapViewWaitingPayOrders:(UIGestureRecognizer *)tap {

    debugMethod();
    WaitPayOrderViewController *waitPayOrderVC = [[WaitPayOrderViewController alloc] init];
    [waitPayOrderVC setHidesBottomBarWhenPushed:true];
    [self.navigationController pushViewController:waitPayOrderVC animated:true];
}

- (void)viewWaitingConfirm:(UIGestureRecognizer *)tap {

    debugMethod();
    WaitVerifyOrderViewController *waitVerfifyOrderVC = [[WaitVerifyOrderViewController alloc] init];
    [waitVerfifyOrderVC setHidesBottomBarWhenPushed:true];
    [self.navigationController pushViewController:waitVerfifyOrderVC animated:true];
}

- (void)tapViewAllOrders:(UIGestureRecognizer *)tap {

    debugMethod();
    WaitCancleViewController *waitCancleOrderVC = [[WaitCancleViewController alloc] init];
    [waitCancleOrderVC setHidesBottomBarWhenPushed:true];
    [self.navigationController pushViewController:waitCancleOrderVC animated:true];
}
- (void)tapViewChangePsd:(UIGestureRecognizer *)tap {
 
    ChangePsdViewController *changePsdVC = [[ChangePsdViewController alloc] init];
    [changePsdVC setHidesBottomBarWhenPushed:true];
    [self.navigationController pushViewController:changePsdVC animated:true];
}

- (void)tapViewUserInfo:(UIGestureRecognizer *)tap {

    StoreInfoViewController * storeInfoVC = [[StoreInfoViewController alloc] init];
    [storeInfoVC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:storeInfoVC animated:true];
}


- (void)tapViewConsumerHotline:(UIGestureRecognizer *)tap {
    
    debugMethod();
    
//    __weak typeof(self) weakself = self;

    UIAlertController *alerController = [UIAlertController alertControllerWithTitle:@"400-658-0550" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"呼叫" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://400-658-0550"]];
    }];
    
    [alerController addAction:cancleAction];
    [alerController addAction:defaultAction];
    [self  presentViewController:alerController animated:YES completion:nil];
}

- (void)removeLoginSession {
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"kCookie"];
    [defaults synchronize];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
